package com.corgi.webservice.endpoint;

import com.corgi.webservice.domain.User;
import com.corgi.webservice.domain.UserIdRequest;
import com.corgi.webservice.domain.UserResponse;
import com.corgi.webservice.repository.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/**
 * @title: UserServiceEndpoint
 * @description: 用户服务Endpoint
 * @author: dengmiao
 * @create: 2019-10-11 17:14
 **/
@Slf4j
@Endpoint
public class UserServiceEndpoint {

	@Autowired
	private UserRepository userRepository;

	@PayloadRoot(namespace = "http://www.segmentfault.com/schemas", localPart = "UserIdRequest")
	@ResponsePayload
	public UserResponse getUser(@RequestPayload UserIdRequest req) {
		long userId = req.getUserId();
		User user = userRepository.findById(userId);

		Instant instant = Instant.ofEpochMilli(req.getTimestamp());
		ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
		log.info("webservice 用户ID {}, 请求时间 {}", userId, zonedDateTime);

		UserResponse resp = new UserResponse();
		resp.setUser(user);
		resp.setTimestamp(Instant.now().toEpochMilli());
		return resp;
	}
}
